﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity = VietStar.Library.Entity;
using Business = VietStar.Library.Business;
using Module = VietStar.Library.Commom;
public partial class controls_client_updateaccount : System.Web.UI.UserControl
{
    Entity.ThanhVien clsE_ThanhVien = new VietStar.Library.Entity.ThanhVien();
    Business.ThanhVien clsB_ThanhVien = new VietStar.Library.Business.ThanhVien();
    Module.Cryption.MD5Cryption md5 = new VietStar.Library.Commom.Cryption.MD5Cryption();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            clsE_ThanhVien.TenDangNhap = Session["user_login_client"].ToString();
            DataTable dt= clsB_ThanhVien.SelectPrimarykey(clsE_ThanhVien);
            txtHoTen.Text = dt.Rows[0]["TenDayDu"].ToString();
            txtDiaChi.Text = dt.Rows[0]["DiaChi"].ToString();
            bdpNgaySinh.SelectedDate = Convert.ToDateTime(dt.Rows[0]["NgaySinh"].ToString());
            txtEmail.Text = dt.Rows[0]["Email"].ToString();
            txtSoDienThoai.Text = dt.Rows[0]["SoDienThoai"].ToString();
            lbTenDangNhap.Text = Session["user_login_client"].ToString();
            txtPassword.Text = "";
            txtPassNew.Text = "";
            txtXacNhanMatKhauMoi.Text = "";            
        }
    }
    private bool kiemtra()
    {
        bool kiemtra = true;
        clsE_ThanhVien.TenDangNhap = lbTenDangNhap.Text;
        DataTable dt = clsB_ThanhVien.SelectPrimarykey(clsE_ThanhVien);
        if (txtPassNew.Text !="" && md5.Encrypt(txtPassword.Text) != dt.Rows[0]["MatKhau"].ToString())
        {
            Alert.Show("Mật khẩu cũ không đúng !!!");
            kiemtra = false;
        }
        else if (txtPassNew.Text != txtXacNhanMatKhauMoi.Text)
        {
            Alert.Show("Xác nhận mật khẩu mới không chính xác !!!");
            kiemtra = false;
        }
        else if (txtPassNew.Text == "")
        {
            clsE_ThanhVien.MatKhau = dt.Rows[0]["MatKhau"].ToString();
        }
        return kiemtra;
    }
    protected void btnSua_Click(object sender, EventArgs e)
    {
        bool save = false;
        try
        {
            if (kiemtra())
            {
                clsE_ThanhVien.TenDayDu = txtHoTen.Text;
                clsE_ThanhVien.DiaChi = txtDiaChi.Text;
                clsE_ThanhVien.Email = txtEmail.Text;
                clsE_ThanhVien.SoDienThoai = txtSoDienThoai.Text;
                clsE_ThanhVien.NgaySinh = bdpNgaySinh.SelectedDate;
                clsE_ThanhVien.TenDangNhap = lbTenDangNhap.Text;
                clsE_ThanhVien.QuyenTruyCap = 4;
                if (txtPassNew.Text != "")
                {
                    clsE_ThanhVien.MatKhau = md5.Encrypt(txtPassNew.Text);
                }
                clsE_ThanhVien.TrangThai = true;
                save = clsB_ThanhVien.Update(clsE_ThanhVien);
                if (save)
                { Alert.Show("Cập nhật thông tin thành công !!!"); }
                else
                { Alert.Show("Cập nhật thông tin không thành công !!!"); }
            }
        }
        catch(Exception ex)
        {
            Alert.Show(ex.ToString());
        }
    }
}
